1931C - Make Equal Again - CodeForces Solution


brute force greedy math

Please click on ads to support us..

Python Code:

t = int(input()) 
for _ in range(t):
    n = int(input())
    conts = [int(i) for i in input().split()]
    elem = conts[0]
    i = 1
    while i < len(conts) and conts[i] == elem :
        i += 1

    end_elem = conts[len(conts) - 1]

    j = len(conts) - 2
    while conts[j] == end_elem and j >= 0:
        j -= 1
    chosen = 0
    if end_elem == elem:
        print(len(conts[i : j + 1]))
    else:
        if i >= len(conts) - 1 - j:
            print(len(conts[i:]))
        else:
            print(len(conts[:j + 1]))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
long long T,n,a[200005];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--){
		cin>>n;
		for(int i=1;i<=n;i++)	cin>>a[i];
		long long l=1,r=n;
		while(a[l]==a[1]&&l<=n)	l++;
		while(a[r]==a[n]&&r>=l)	r--;
		if(a[1]!=a[n])	cout<<min(n-l+1,r)<<"\n";
		else	cout<<r-l+1<<"\n";
	}
}


Comments

Submit
0 Comments
More Questions

979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits